"""
is 和 ==
- == 用来比较两个对象的值是否相等，适用所有对象
- is 用于比较两个对象是否是同一个对象，即它们是否指向内存中的同一位置。
"""
import sys


def example1():
    a = [1, 2, [3, 4]]
    b = [1, 2, [3, 4]]

    print(a == b)

    print(id(a))
    print(id(b))
    print(a is b)


# 小整数和字符串的缓存
def example2():
    a1 = 256
    b1 = 256

    print(a1 is b1)  # 输出: True

    a2 = 257
    b2 = 257
    print(id(a2))
    print(id(b2))
    # a is b 返回 True 是因为 CPython 可能已经动态扩展了整数缓存范围，使得 257 也被缓存了
    print(a2 is b2)  # 正常输出: False
    print(-sys.int_info[0] <= 257 < sys.int_info[0])  # 输出: False
    print(sys.int_info)

    s1 = "hello"
    s2 = "hello"

    print(s1 is s2)  # 输出: True

    s1 = "hello world"
    s2 = "hello world"

    print(s1 is s2)  # 输出: False
    # 检查256-300 整数是否被缓存
    for i in range(256, 300):
        a = i
        b = i
        if a is not b:
            print(f"Integer {i} is not cached.")
            break
    else:
        print("All integers in the tested range are cached.")


example2()
